<template>
    <div class="rx-jurisdiction">
        <div>
            <a-radio style="width: 80px" @change="onChangeAll" v-model="allType">{{i18n('FormBoListEditPermissionConf.all','所有人')}}</a-radio>
            <a-radio style="width: 80px" @change="onChangeShowList" v-model="showList">{{i18n('FormBoListEditPermissionConf.custom','自定义')}}</a-radio>
        </div>
        <template v-if="showList">
            <a-alert style="margin: 15px 0px 5px 0px" :message="i18n('FormBoListEditPermissionConf.authDescription','如果勾选了不包含，是除开选中的都有权限，不勾选则选中的有权限')"
                     type="info" show-icon/>
            <ul>
                <li v-for="(dataObj, index) in typeList">
                    <form-user-or-group style="margin-top: 10px" :show="false" @change="aliskt" ref="preview"
                                        :dataObj="dataObj"></form-user-or-group>
                </li>
            </ul>
        </template>
    </div>
</template>

<script>
import FormPermissionApi from "@/api/form/core/formPermission";
import FormUserOrGroup from "@/views/modules/form/core/formPermission/FormUserOrGroup";
export default {
    name: "rx-jurisdiction",
    data(){
        return{
            typeList: [],
            allType: false,//所有人
            showList: false,//自定义
        }
    },
    props: ['value'],
    components:{
        FormUserOrGroup
    },
    mounted(){
        this.init(this.value)
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"FormBoPmt",key);
        },
        init(values){
            debugger
            var self = this;
            FormPermissionApi.getPermissionList().then((res) => {
                self.setTypeValue(res.data,values.value);
            })
        },
        onChangeShowList(e){
            if(this.showList){
                this.allType = false;
                if(this.attachmentType){
                    this.attachmentList = true;
                }
            }
            this.changeType();
        },
        onChangeAll() {
            if (this.allType) {
                this.showList = false;
            }
            this.changeType()
        },
        changeType() {
            for (let i = 0; i < this.typeList.length; i++) {
                let obj = this.typeList[i]
                obj.values = ''
                obj.names = ''
            }
            this.getValues()
        },
        setTypeValue(data, value) {
            this.typeList = data
            if (!value || 'everyone' == value) {
                this.allType = true
                this.onChangeAll()
            } else if ('custom' == value) {
                this.showList = true
            } else {
                var selectObj = JSON.parse(value)
                for (var i = 0; i < this.typeList.length; i++) {
                    var typeObj = this.typeList[i]
                    var isContinue = false
                    for (var key in selectObj) {
                        if (key == typeObj.type) {
                            typeObj.values = selectObj[key].values
                            typeObj.names = selectObj[key].names
                            typeObj.include = selectObj[key].include
                            isContinue = true
                            break
                        }
                    }
                    if (isContinue) {
                        continue
                    }
                }
                this.showList = true
            }
        },
        aliskt(){
            this.getValues()
        },
        getValues() {
            if (this.allType) {
                var resultData = {
                    label: '所有人',
                    value: 'everyone'
                }
            } else {
                var names = []
                var typeListObj = {}
                for (var i = 0; i < this.typeList.length; i++) {
                    var typeObj = this.typeList[i]
                    if (!typeObj.names) {
                        continue
                    }
                    typeListObj[typeObj.type] = {
                        names: typeObj.names,
                        values: typeObj.values,
                        include: typeObj.include,
                    }
                    names.push(typeObj.name + ':' + typeObj.names)
                }

                if (names.length > 0) {
                    var resultData = {
                        label: names.join(';'),
                        value: JSON.stringify(typeListObj),
                    }
                } else {
                    var resultData = {
                        label: "无配置",
                        value: 'custom'
                    }
                }
            }
            this.$emit("input",resultData);
        }
    }
}
</script>

<style scoped>

</style>